package cn.timeapi.Applet.Mall.CatSquare.util;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 描述:Http请求相关的工具
 *
 * @author Vincent Vic
 * create 2020-03-14 22:00
 */


public class HttpUtil {


    private static final String ERROR_IP = "null";

    /**
     *  @author HuaZai	 * @contact who.seek.me@java98k.vip	 * @title getUserIP
     * <ul>	  @description 获取用户真实的IP地址	      </ul>
     *  @createdTime 2017年12月30日 下午6:42:17
     *  @param request
     *  @return String
     *  @version : V1.0.0
     */
    public static String getUserIP(HttpServletRequest request) {
        // 优先取 X-Real-IP
        String ip = request.getHeader("X-Real-IP");
        System.out.println(ip);
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("x-forwarded-for");
        }
        System.out.println(ip);
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
            if ("0:0:0:0:0:0:0:1".equals(ip)) {
                ip = ERROR_IP;
            }
        }
        System.out.println(ip);
        if ("unknown".equalsIgnoreCase(ip)) {
            ip = ERROR_IP;
            return ip;
        }

        if (ip == null)
            return ip;
        int index = ip.indexOf(',');
        if (index >= 0) {
            ip = ip.substring(0, index);
        }
        return ip;
    }


    /**
     *
     * @author HuaZai
     * @contact who.seek.me@java98k.vip
     * @title getServerIP
     *        <ul>
     * @description 获取服务器IP地址
     *              </ul>
     * @createdTime 2017年12月30日 下午7:44:16
     * @return String
     *
     * @version : V1.0.0
     */
    public static String getServerIP(){
        InetAddress inetAddress;
        try {
            inetAddress = InetAddress.getLocalHost();
            String hostAddress = inetAddress.getHostAddress();
            return hostAddress;
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        return ERROR_IP;

    }



    /**
     * @author HuaZai
     * @contact who.seek.me@java98k.vip
     * @title getRemoteIp
     *        <ul>
     * @description 获取远程IP地址
     *              </ul>
     * @createdTime 2017年12月30日 下午6:39:22
     * @param request
     * @return String
     *
     * @version : V1.0.0
     */

    public static String getRemoteIp(HttpServletRequest request) {
        String ip = request.getHeader("x-real-ip");
        System.out.println(ip);
        if (ip == null)
            ip = request.getRemoteAddr();
        // 过滤反向代理的IP
        String[] stemps = ip.split(",");
        for (int i = 0; i < stemps.length; i++) {
            System.out.println(stemps[i]);
        }

        if (stemps != null && stemps.length >= 1) {
            // 得到第一个IP，即客户端真实IP
            ip = stemps[0];
        }

        System.out.println("mygetre:"+getUserIP(request));
        ip = ip.trim();
        if (ip.length() > 23) {
            ip = ip.substring(0, 23);
        }
        if (ip.equals("127.0.0.1")) {
            ip = request.getHeader("x-forwarded-for");
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            System.out.println(ip);
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            System.out.println(ip);
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }
            System.out.println(ip);
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            System.out.println(ip);
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
            System.out.println(ip);
        }

        return ip;
    }


}